<?php
/**
 * Laconica, the distributed open-source microblogging tool
 *
 * Base class for all actions (~views)
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Action
 * @package   Laconica
 * @author    Evan Prodromou <evan@controlyourself.ca>
 * @author    Sarven Capadisli <csarven@controlyourself.ca>
 * @copyright 2008 Control Yourself, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://laconi.ca/
 */

if (!defined('LACONICA')) {
    exit(1);
}

require_once INSTALLDIR.'/lib/noticeform.php';
require_once INSTALLDIR.'/lib/htmloutputter.php';

/**
 * Base class for all actions
 *
 * This is the base class for all actions in the package. An action is
 * more or less a "view" in an MVC framework.
 *
 * Actions are responsible for extracting and validating parameters; using
 * model classes to read and write to the database; and doing ouput.
 *
 * @category Output
 * @package  Laconica
 * @author   Evan Prodromou <evan@controlyourself.ca>
 * @author   Sarven Capadisli <csarven@controlyourself.ca>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://laconi.ca/
 *
 * @see      HTMLOutputter
 */
class Action extends HTMLOutputter // lawsuit
{
    var $args;

    /**
     * Constructor
     *
     * Just wraps the HTMLOutputter constructor.
     *
     * @param string  $output URI to output to, default = stdout
     * @param boolean $indent Whether to indent output, default true
     *
     * @see XMLOutputter::__construct
     * @see HTMLOutputter::__construct
     */
    function __construct($output='php://output', $indent=true)
    {
        parent::__construct($output, $indent);
    }

    /**
     * For initializing members of the class.
     *
     * @param array $argarray misc. arguments
     *
     * @return boolean true
     */
    function prepare($argarray)
    {
        $this->args =& common_copy_args($argarray);
        return true;
    }

    /**
     * Show page, a template method.
     *
     * @return nothing
     */
    function showPage()
    {
        $this->startHTML();
        $this->showHead();
        $this->showBody();
        $this->endHTML();
    }

    /**
     * Show head, a template method.
     *
     * @return nothing
     */
    function showHead()
    {
        // XXX: attributes (profile?)
        $this->elementStart('head');
        $this->showTitle();
        $this->showStylesheets();
        $this->showScripts();
        $this->showOpenSearch();
        $this->showFeeds();
        $this->showDescription();
        $this->extraHead();
        $this->showThemeByUser();
        $this->elementEnd('head');
    }

    function showThemeByUser()
    {
        //Need to check one more page, notice details
        $user_page = common_current_user();
        if($this->arg('notice'))
        {
            $id = $this->arg('notice');
            $notice_temp = Notice::staticGet($id);
            $user_page = User::staticGet('id', $notice_temp->profile_id);
        }
        if($this->arg('nickname'))
        {
            if((strpos($this->arg('action'),'group',0)===false) || $this->arg('action')=='usergroups')
            {
                $nickname = common_canonical_nickname($this->arg('nickname'));
                $user_page = User::staticGet('nickname', $nickname);
            }
        }
        if($user_page)
        {
                $profile = $user_page->getProfile();
                $current_theme_id = $profile->user_theme;
                if($current_theme_id<>0)
                {       
                    $theme_show =  Custom_theme::staticGet('id',$current_theme_id );
                    $this->raw($theme_show->showAll());
                }
				   else
        {
            $this->raw('<style type="text/css">#wrap{background:transparent url(http://toi24h.com/images/footer2.png) no-repeat scroll center bottom;} #overall{ background-image:url(http://toi24h.com/images/header3.jpg);
background-position:center top;
background-repeat:no-repeat;} #footer{margin-top:200px;}</style>');
        }
        }
        else
        {
            $this->raw('<style type="text/css">#wrap{background:transparent url(http://toi24h.com/images/footer2.png) no-repeat scroll center bottom;} #overall{ background-image:url(http://toi24h.com/images/header3.jpg);
background-position:center top;
background-repeat:no-repeat;} #footer{margin-top:200px;}</style>');
        }

        
    }
    /**
     * Show title, a template method.
     *
     * @return nothing
     */
    function showTitle()
    {
        $this->element('title', null,
                       sprintf(_("%s - %s"),
                               $this->title(),
                               common_config('site', 'name')));
    }

    /**
     * Returns the page title
     *
     * SHOULD overload
     *
     * @return string page title
     */

    function title()
    {
        return _("Untitled page");
    }

    /**
     * Show stylesheets
     *
     * @return nothing
     */
    function showStylesheets()
    {
                $this->element('link', array('rel' => 'shortcut icon',
                                     'href' => 'http://toi24h.com/favicon.ico'));
                                                     $this->element('meta', array('http-equiv' => 'refresh',
                                     'content' => '600'));
                $this->element('meta', array('http-equiv' => 'Content-Type',
                                     'content' => 'text/html; charset=UTF-8'));
                $this->element('meta', array('name' => 'description',
                                     'content' => 'Tôi 24h, micro-blog 140 ký tự tại Việt Nam. Hãy tham gia để chia sẻ nhanh những thông tin mới từ bạn với bạn bè, gia đình và đồng nghiệp !'));
                $this->element('meta', array('name' => 'keywords',
                                     'content' => 'micro-blog, tiểu blog, 140 ký tự, micro-blog Việt Nam, twitter Việt Nam, tin nhanh, bạn bè, gia đình, đồng nghiệp'));
                $this->element('meta', array('name' => 'robots',
                                     'content' => 'noindex,follow'));
                                     
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/display.css', 'base') . '?version=' . LACONICA_VERSION,
                                     'media' => 'screen, projection, tv'));
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/display.css', null) . '?version=' . LACONICA_VERSION,
                                     'media' => 'screen, projection, tv'));  
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/jquery.contextMenu.css', null))); 
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/thickbox.css', null))); 
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/avim.css', null))); 									 
        $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
                       'href="'.theme_path('css/ie.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
        foreach (array(6,7) as $ver) {
            if (file_exists(theme_file('css/ie'.$ver.'.css', 'base'))) {
                // Yes, IE people should be put in jail.
                $this->comment('[if IE '.$ver.']><link rel="stylesheet" type="text/css" '.
                               'href="'.theme_path('css/ie'.$ver.'.css', 'base').'?version='.LACONICA_VERSION.'" /><![endif]');
            }
        }
    }

    /**
     * Show javascript headers
     *
     * @return nothing
     */
    function showScripts()
    {

        $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/jquery.min.js')),
                       ' ');  
        $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/jquery.form.js')),
                       ' ');
        $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/xbImportNode.js')),
                       ' ');
        $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
                       ' ');  
       $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/jquery.contextMenu.js')),
                       ' ');
       $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/thickbox.js')),
                       ' ');
	   $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/avim.js')),
                       ' ');
       $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/jquery.filedselection.js?version')),
                       ' ');
       $this->element('script', array('type' => 'text/javascript',
                                       'src' => common_path('js/jquery.popupWindow.js')),
                       ' ');                  
    }

    /**
     * Show OpenSearch headers
     *
     * @return nothing
     */
    function showOpenSearch()
    {
        $this->element('link', array('rel' => 'search',
                                     'type' => 'application/opensearchdescription+xml',
                                     'href' =>  common_local_url('opensearch', array('type' => 'people')),
                                     'title' => common_config('site', 'name').' People Search'));
        $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
                                     'href' =>  common_local_url('opensearch', array('type' => 'notice')),
                                     'title' => common_config('site', 'name').' Notice Search'));
    }

    /**
     * Show feed headers
     *
     * MAY overload
     *
     * @return nothing
     */
    function showFeeds()
    {
        // does nothing by default
    }

    /**
     * Show description.
     *
     * SHOULD overload
     *
     * @return nothing
     */
    function showDescription()
    {
        // does nothing by default
    }

    /**
     * Show extra stuff in <head>.
     *
     * MAY overload
     *
     * @return nothing
     */
    function extraHead()
    {
        // does nothing by default
    }

    /**
     * Show body.
     *
     * Calls template methods
     *
     * @return nothing
     */
    function showBody()
    {
        $this->elementStart('body', array('id' => $this->trimmed('action')));
        $this->elementStart('div', array('id' => 'overall'));
        $this->elementStart('div', array('id' => 'wrap'));
        $this->showHeader();
        $this->showCore();
        $this->showFooter();
        $this->elementEnd('div');
        $this->elementEnd('div');
        $this->elementEnd('body');
    }

    /**
     * Show header of the page.
     *
     * Calls template methods
     *
     * @return nothing
     */
    function showHeader()
    {
        $this->elementStart('div', array('id' => 'header'));
        $this->showLogo();
        $this->showPrimaryNav();
        $this->showSiteNotice();
        if (common_logged_in()) {
           // $this->elementStart('div', array('id' => 'divWelcome'));
          //  $this->showLabelWelcome();
           // $this->showToolBar();

            $this->showNoticeForm();
           // $this->elementEnd('div');
        } else {
            $this->showAnonymousMessage();
        }
        //$this->elementEnd('div');
    }
    
    function showLabelWelcome()
    {
        $user = common_current_user();
        
        $this->element('label', array('for' => 'notice_data-text', 'class'=>'labelWelcome'),
                            sprintf(_('What\'s up, %s?'), $user->nickname));

    }
    
    function showToolBar()
    {
    
        $this->elementStart('div', array('id' => 'toolbar'));
         $this->elementStart('ul');
         $this->elementStart('li');
            $this->raw("<a href='#' id='imagetool' title='Gửi kèm ảnh vào tin nhắn'><span class='menutext'>Gửi ảnh</span></a> ");
            $this->raw("<a href='#' id='maptool' title='Đính kèm bản đồ vào tin nhắn'><span class='menutext'>Bản đồ</span></a> ");
            $this->raw("<a href='#' id='icontool' title='Hiển thị biểu tượng, icon, mặt cười'><span class='menutext'>Icon</span></a> ");
         $this->elementEnd('li');
/*        $this->raw("<div id='toolmenu'><img src='images/tools/phone_52.gif'></img>&nbsp;Hình ảnh&nbsp;&nbsp;&nbsp; <img src='images/tools/icon_world.gif'></img>&nbsp;Bản đồ&nbsp;&nbsp;&nbsp; <img src='images/tools/icon_user.gif'></img>&nbsp;Icon</div>");*/
        $this->elementEnd('ul');
        $this->elementStart('div', array('id' => 'uploadBar'));
        $this->elementStart('form', array('enctype' => 'multipart/form-data',
                                            'method' => 'post',
                                           'id' => 'form_upload_image',
                                           'class' => 'form_settings',
                                           'action' => common_local_url('imageupload')));
        $this->hidden('token', common_session_token());
        $this->inputUpload('image_notice_upload', _('Chọn ảnh'),null,null);
        $this->element('input', array('name' => 'MAX_FILE_SIZE',
                                      'type' => 'hidden',
                                      'id' => 'MAX_FILE_SIZE',
                                      'value' => ImageFile::maxFileSizeInt()));
        $this->submit('upload', _('Upload'));
        $this->elementEnd('form');
        $this->elementEnd('div');
        $this->elementStart('div', array('id' => 'mapBar'));
        $this->raw('<p>Cấu trúc bản đồ : [bando] Số nhà, Tên đường, Phường, Quận, Thành phố [/bando]<br/>Ví dụ : [bando]1, Cong Xa Paris, P.Ben Nghe, 1, TpHCM[/bando]</p>');
        $this->elementEnd('div');
        $this->elementEnd('div');
    }

    /**
     * Show configured logo.
     *
     * @return nothing
     */
    function showLogo()
    {
        $this->elementStart('address', array('id' => 'site_contact',
                                             'class' => 'vcard'));
        $this->elementStart('a', array('class' => 'url home bookmark',
                                       'href' => common_local_url('public')));
        if (common_config('site', 'logo') || file_exists(theme_file('logo.png'))) {
            $this->element('img', array('class' => 'logo photo',
                                        'src' => (common_config('site', 'logo')) ? common_config('site', 'logo') : theme_path('logo.png'),
                                        'alt' => common_config('site', 'name')));
        }
        $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
        $this->elementEnd('a');
        $this->elementEnd('address');
    }

    /**
     * Show primary navigation.
     *
     * @return nothing
     */
    function showPrimaryNav()
    {
        $this->elementStart('dl', array('id' => 'site_nav_global_primary'));
        $this->element('dt', null, _('Primary site navigation'));
        $this->elementStart('dd');
        $user = common_current_user();
        $this->elementStart('ul', array('class' => 'nav'));
          $this->menuItem(common_local_url('public', array('title' => 'Cộng đồng')),
                        _('Cộng đồng'), _('Trang tin cộng đồng, họ đang làm gì?'), false, 'nav_public');
        if ($user) {
            $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
                            _('Trang cá nhân'), _('Personal profile and friends timeline'), false, 'nav_home');
            $this->menuItem(common_local_url('subscriptions',array('nickname' => $user->nickname)),
                            _('Bạn bè'), _('Danh sách bạn bè của bạn'), false, 'nav_account');
        }
        $this->menuItem(common_local_url('friendsearch'),
                        _('Search'), _('Search for people or text'), false, 'nav_search');
        if ($user) {
            $this->menuItem(common_local_url('profilesettings'),
                            _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
            $this->menuItem(common_local_url('imsettings'),
                            _('Kết nối'), _('Cập nhật tin qua Y!M/GTalk, Twitter,...'), false, 'nav_connect');

            /*if (common_config('xmpp', 'enabled')) {
                $this->menuItem(common_local_url('imsettings'),
                            _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
            } else {
                $this->menuItem(common_local_url('smssettings'),
                            _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
            }*/
            $this->menuItem(common_local_url('logout'),
                            _('Logout'), _('Logout from the site'), false, 'nav_logout');
        } else {
            $this->menuItem(common_local_url('login'),
                            _('Login'), _('Login to the site'), false, 'nav_login');
            if (!common_config('site', 'closed')) {
                $this->menuItem(common_local_url('register'),
                                _('Register'), _('Create an account'), false, 'nav_register');
            }
            $this->menuItem(common_local_url('openidlogin'),
                            _('OpenID'), _('Login with OpenID'), false, 'nav_openid');
        }
        $this->menuItem(common_local_url('doc', array('title' => 'help')),
                        _('Help'), _('Help me!'), false, 'nav_help');
        $this->elementEnd('ul');
        $this->elementEnd('dd');
        $this->elementEnd('dl');
       // $this->raw("<div style='position:absolute; left:750px; top: 50px; width:200px; color:red; font-weight:bold'> Bạn có thể chỉnh sửa theme của bạn? Bạn có biết không? <a href='http://toi24h.com/doc/theme' title='Hướng dẫn thay đổi theme Tôi24h'>Thử ngay</a><br />Cập nhật tin nhanh qua <a href='http://toi24h.com/settings/im' title='Cập nhật gởi và nhận tin nhanh qua IM - Yahoo Messenger GTalk'>Y!M</a></div>");
   
    }

    /**
     * Show site notice.
     *
     * @return nothing
     */
    function showSiteNotice()
    {
        // Revist. Should probably do an hAtom pattern here
        $text = common_config('site', 'notice');
        if ($text) {
            $this->elementStart('dl', array('id' => 'site_notice',
                                            'class' => 'system_notice'));
            $this->element('dt', null, _('Site notice'));
            $this->elementStart('dd', null);
            $this->raw($text);
            $this->elementEnd('dd');
            $this->elementEnd('dl');
        }
    }

    /**
     * Show notice form.
     *
     * MAY overload if no notice form needed... or direct message box????
     *
     * @return nothing
     */
    function showNoticeForm()
    {
        $notice_form = new NoticeForm($this);
        $notice_form->show();
    }

    /**
     * Show anonymous message.
     *
     * SHOULD overload
     *
     * @return nothing
     */
    function showAnonymousMessage()
    {
        // needs to be defined by the class
    }

    /**
     * Show core.
     *
     * Shows local navigation, content block and aside.
     *
     * @return nothing
     */
    function showCore()
    {
        $this->elementStart('div', array('id' => 'core'));
        $this->showLocalNavBlock();
        $this->showContentBlock();
        $this->showAside();
        $this->elementEnd('div');
    }

    /**
     * Show local navigation block.
     *
     * @return nothing
     */
    function showLocalNavBlock()
    {
        $this->elementStart('dl', array('id' => 'site_nav_local_views'));
        $this->element('dt', null, _('Local views'));
        $this->elementStart('dd');
        $this->showLocalNav();
        $this->elementEnd('dd');
        $this->elementEnd('dl');
    }

    /**
     * Show local navigation.
     *
     * SHOULD overload
     *
     * @return nothing
     */
    function showLocalNav()
    {
        // does nothing by default
    }

    /**
     * Show content block.
     *
     * @return nothing
     */
    function showContentBlock()
    {
        $this->elementStart('div', array('id' => 'content'));
        $this->showPageTitle();
        $this->showPageNoticeBlock();
        $this->elementStart('div', array('id' => 'content_inner'));
        // show the actual content (forms, lists, whatever)
        $this->showContent();
        $this->elementEnd('div');
        $this->elementEnd('div');
    }

    /**
     * Show page title.
     *
     * @return nothing
     */
    function showPageTitle()
    {
        $this->element('h1', null, $this->title());
    }

    /**
     * Show page notice block.
     *
     * @return nothing
     */
    function showPageNoticeBlock()
    {
        $this->elementStart('dl', array('id' => 'page_notice',
                                        'class' => 'system_notice'));
        $this->element('dt', null, _('Page notice'));
        $this->elementStart('dd');
        $this->showPageNotice();
        $this->elementEnd('dd');
        $this->elementEnd('dl');
    }

    /**
     * Show page notice.
     *
     * SHOULD overload (unless there's not a notice)
     *
     * @return nothing
     */
    function showPageNotice()
    {
    }

    /**
     * Show content.
     *
     * MUST overload (unless there's not a notice)
     *
     * @return nothing
     */
    function showContent()
    {
    }

    /**
     * Show Aside.
     *
     * @return nothing
     */
    function showAside()
    {
        $this->elementStart('div', array('id' => 'aside_primary',
                                         'class' => 'aside'));
        $this->elementStart('h2');
        $this->raw('<a href="http://toi24h.com/images/quicktour.html" class="quicktourlink" title="Hướng dẫn sử dụng nhanh các tính năng của Tôi24h"><img src="http://toi24h.com/images/quicktour.png" ></img></a>');
        $this->raw("<script type='text/javascript'> 
                        $('.quicktourlink').popupWindow({ 
                        height:600, 
                        width:800, 
                        top:50, 
                        left:200 
                    }); 
                </script>");
        $this->elementEnd('h2');
        $this->showExportData();
        $this->showSections();
        $this->elementEnd('div');
    }

    /**
     * Show export data feeds.
     *
     * MAY overload if there are feeds
     *
     * @return nothing
     */
    function showExportData()
    {
        // is there structure to this?
        // list of (visible!) feed links
        // can we reuse list of feeds from showFeeds() ?
    }

    /**
     * Show sections.
     *
     * SHOULD overload
     *
     * @return nothing
     */
    function showSections()
    {
        // for each section, show it
    }

    /**
     * Show footer.
     *
     * @return nothing
     */
    function showFooter()
    {
 
        $this->raw("<ul id='myMenu' class='contextMenu'><li><a href='#maybay'><img src='http://toi24h.com/life/maybay.gif' title='đi công tác, du lịch, máy bay' /></a></li><li><a href='#baothuc'><img src='http://toi24h.com/life/baothuc.gif'  title='đồng hồ, đến giờ, hẹn giờ'  /></a></li><li><a href='#contrai'><img src='http://toi24h.com/life/contrai.gif'  title='con trai'  /></a></li><li><a href='#congai'><img src='http://toi24h.com/life/congai.gif'  title='con gái'/></a></li><li><a href='#bongbong'><img src='http://toi24h.com/life/bongbong.gif'  title='vui chơi, tiệc tùng, bong bóng'  /></a></li><li><a href='#bithuong'><img src='http://toi24h.com/life/bithuong.gif'  title='bị thương, tai nạn, đau, ê ẩm'  /></a></li><li><a href='#raochan'><img src='http://toi24h.com/life/raochan.gif'  title='cấm, rào cản, chắn'  /></a></li><li><a href='#bongro'><img src='http://toi24h.com/life/bongro.gif'  title='thể thao, bóng rổ'  /></a></li> <li><a href='#nhau'><img src='http://toi24h.com/life/nhau.gif'  title='ăn nhậu, tám'  /></a></li><li><a href='#xedap'><img src='http://toi24h.com/life/xedap.gif'  title='đi, xe đạp'  /></a></li><li><a href='#ongnhom'><img src='http://toi24h.com/life/ongnhom.gif' title='xem, ống nhòm, tò mò' /></a></li><li><a href='#patin'><img src='http://toi24h.com/life/patin.gif' title='đi, patin' /></a></li><li><a href='#naucom'><img src='http://toi24h.com/life/naucom.gif' title='nấu cơm, đồ ăn' /></a></li><li><a href='#bomb'><img src='http://toi24h.com/life/bomb.gif' title='Bùm' /></a></li><li><a href='#docsach'><img src='http://toi24h.com/life/docsach.gif' title='thư giãn, đọc sách' /></a></li><li><a href='#aonguc'><img src='http://toi24h.com/life/aonguc.gif' title='áo ngực' /></a></li><li><a href='#xebus'><img src='http://toi24h.com/life/xebus.gif' title='đi, xe bus' /></a></li><li><a href='#chupanh'><img src='http://toi24h.com/life/chupanh.gif' title='chụp ảnh' /></a></li><li><a href='#xehoi'><img src='http://toi24h.com/life/xehoi.gif' title='đi, xe hơi' /></a></li><li><a href='#casette'><img src='http://toi24h.com/life/casette.gif' title='băng casette' /></a></li><li><a href='#anmung'><img src='http://toi24h.com/life/anmung.gif' title='ăn mừng' /></a></li><li><a href='#hutthuoc'><img src='http://toi24h.com/life/hutthuoc.gif' title='hút thuốc, không có lợi cho sức khoẻ' /></a></li><li><a href='#laudon'><img src='http://toi24h.com/life/laudon.gif' title='lau, dọn dẹp, sạch' /></a></li><li><a href='#xemdongho'><img src='http://toi24h.com/life/xemdongho.gif' title='xem giờ, đồng hồ' /></a></li><li><a href='#deptroi'><img src='http://toi24h.com/life/deptroi.gif' title='ngày đẹp trời, có mây ' /></a></li><li><a href='#cafe'><img src='http://toi24h.com/life/cafe.gif' title='cà phê, nhâm nhi, relax' /></a></li><li><a href='#lamviec'><img src='http://toi24h.com/life/lamviec.gif' title='làm việc, máy tính'  /></a></li><li><a href='#bowling'><img src='http://toi24h.com/life/bowling.gif' title='thể thao, bowling' /></a></li><li><a href='#dulieu'><img src='http://toi24h.com/life/dulieu.gif' title='thông tin, dữ liệu' /></a></li><li><a href='#khoan'><img src='http://toi24h.com/life/khoan.gif' title='khoan, đục, cắt' /></a></li><li><a href='#anuong'><img src='http://toi24h.com/life/anuong.gif' title='ăn uống' /></a></li><li><a href='#sukien'><img src='http://toi24h.com/life/sukien.gif' title='sự kiện' /></a></li><li><a href='#chamhet'><img src='http://toi24h.com/life/chamhet.gif' title='chấm hết!' /></a></li><li><a href='#manggiay'><img src='http://toi24h.com/life/manggiay.gif' title='giày' /></a></li><li><a href='#choidientu'><img src='http://toi24h.com/life/choidientu.gif' title='chơi game, điện tử' /></a></li><li><a href='#gameboy'><img src='http://toi24h.com/life/gameboy.gif' title='máy game, cầm tay, relax' /></a></li><li><a href='#luudanno'><img src='http://toi24h.com/life/luudanno.gif' title='lựu đạn, nổ, phăng' /></a></li><li><a href='#gatgong'><img src='http://toi24h.com/life/gatgong.gif' title='gắt gỏng, bực mình, khó chịu' /></a></li><li><a href='#baytay'><img src='http://toi24h.com/life/bantay.gif' title='bàn tay' /></a></li>
<li><a href='#treodo'><img src='http://toi24h.com/life/treodo.gif' title='phơi quần áo, treo đồ' /></a></li><li><a href='#vuive'><img src='http://toi24h.com/life/vuive.gif' title='vui vẻ, hạnh phúc' /></a></li><li><a href='#langnghe'><img src='http://toi24h.com/life/langnghe.gif' title='lắng nghe' /></a></li><li><a href='#nha'><img src='http://toi24h.com/life/nha.gif' title='nhà' /></a></li><li><a href='#hoiha'><img src='http://toi24h.com/life/hoiha.gif' title='đi, nhanh, hối hả, về' /></a></li><li><a href='#ankem'><img src='http://toi24h.com/life/ankem.gif' title='ăn kem, mát, tận hưởng' /></a></li>
                        <li><a href='#vitinh'><img src='http://toi24h.com/life/vitinh.gif' title='vi tính' /></a></li>
                        <li><a href='#aolanh'><img src='http://toi24h.com/life/aolanh.gif' title='lạnh, áo lạnh' /></a></li>
                        <li><a href='#loa'><img src='http://toi24h.com/life/loa.gif' title='loa, phát thanh' /></a></li>
                        <li><a href='#yeu'><img src='http://toi24h.com/life/yeu.gif' title='yêu, quý, thươnG' /></a></li>
                        <li><a href='#hanhly'><img src='http://toi24h.com/life/hanhly.gif' title='hành lý, khởi hành, vali' /></a></li>
                        <li><a href='#trangdiem'><img src='http://toi24h.com/life/trangdiem.gif' title='trang điểm, làm đẹp, phấn son' /></a></li>
                        <li><a href='#phatthanh'><img src='http://toi24h.com/life/phatthanh.gif' title='phát thanh' /></a></li>
                        <li><a href='#tau'><img src='http://toi24h.com/life/tau.gif' title='tàu lửa' /></a></li>
                        <li><a href='#naunuong'><img src='http://toi24h.com/life/naunuong.gif' title='nấu nướng, nấu ăn' /></a></li>
                        <li><a href='#chui'><img src='http://toi24h.com/life/chui.gif' title='chửi' /></a></li>
                        <li><a href='#buoisang'><img src='http://toi24h.com/life/buoisang.gif' title='buổi sáng' /></a></li>
                        <li><a href='#xemphim'><img src='http://toi24h.com/life/xemphim.gif' title='xem phim, relax, giải trí' /></a></li>
                        <li><a href='#ghichu'><img src='http://toi24h.com/life/ghichu.gif' title='ghi chú, nhắc nhở' /></a></li>
                        <li><a href='#picnic'><img src='http://toi24h.com/life/picnic.gif' title='đi chơi, picnic, dã ngoại' /></a></li>
                        <li><a href='#uongthuoc'><img src='http://toi24h.com/life/uongthuoc.gif' title='bệnh, uống thuốc, không khoẻ' /></a></li>
                        <li><a href='#aothethao'><img src='http://toi24h.com/life/aothethao.gif' title='áo'   /></a></li>
                        <li><a href='#baprangbo'><img src='http://toi24h.com/life/baprangbo.gif' title='bắp rang bơ'  /></a></li>
                        <li><a href='#thacmac'><img src='http://toi24h.com/life/thacmac.gif' title='thắc mắc, muốn hỏi'  /></a></li>
                        <li><a href='#xeday'><img src='http://toi24h.com/life/xeday.gif' title='xe đẩy'  /></a></li>
                        <li><a href='#giavi'><img src='http://toi24h.com/life/giavi.gif' title='nêm gia vị, pha chế'  /></a></li>
                        <li><a href='#daodat'><img src='http://toi24h.com/life/daodat.gif' title='đào, bới, đất'  /></a></li>
                        <li><a href='#daugau'><img src='http://toi24h.com/life/daugau.gif' title='đầu gấu, hung dữ'  /></a></li>
                        <li><a href='#timkiem'><img src='http://toi24h.com/life/timkiem.gif' title='tìm kiếm'  /></a></li>
                        <li><a href='#dicho'><img src='http://toi24h.com/life/dicho.gif' title='bệnhđi chợ, mua hàng, mua bán'  /></a></li>
                        <li><a href='#caulong'><img src='http://toi24h.com/life/caulong.gif' title='thể thao, cầu lông'  /></a></li>
                        <li><a href='#truotvan'><img src='http://toi24h.com/life/truotvan.gif' title='trượt ván' /></a></li>
                        <li><a href='#ngunuong'><img src='http://toi24h.com/life/ngunuong.gif' title='ngủ, khò khò' /></a></li>
                        <li><a href='#lan'><img src='http://toi24h.com/life/lan.gif' title='bơi, lặn' /></a></li>
                        <li><a href='#muadong'><img src='http://toi24h.com/life/muadong.gif' title='mùa đông, lạnh'/></a></li>
                        <li><a href='#uongnuoc'><img src='http://toi24h.com/life/uongnuoc.gif' title='uống nước' /></a></li> 
                        <li><a href='#cahat'><img src='http://toi24h.com/life/cahat.gif' title='ca hát, vui tươi' /></a></li>
                        <li><a href='#xit'><img src='http://toi24h.com/life/xit.gif' title='bình xịt' /></a></li>
                        <li><a href='#ancom'><img src='http://toi24h.com/life/ancom.gif' title='ăn cơm' /></a></li>
                        <li><a href='#troibao'><img src='http://toi24h.com/life/troibao.gif' title='trời mưa, bão' /></a></li>
                        <li><a href='#quanao'><img src='http://toi24h.com/life/quanao.gif' title='quần áo' /></a></li>
                        <li><a href='#mattroi'><img src='http://toi24h.com/life/mattroi.gif' title='mặt trời' /></a></li>
                        <li><a href='#taxi'><img src='http://toi24h.com/life/taxi.gif' title='đi, taxi' /></a></li>
                        <li><a href='#mixnhac'><img src='http://toi24h.com/life/mixnhac.gif' title='mix nhạc' /></a></li>
                        <li><a href='#xemkich'><img src='http://toi24h.com/life/xemkich.gif' title='giải trí, xem kịch' /></a></li>
                        <li><a href='#ansang'><img src='http://toi24h.com/life/ansang.gif' title='ăn sáng' /></a></li>
                        <li><a href='#toilet'><img src='http://toi24h.com/life/toilet.gif' title='Toilet, WC' /></a></li>
                        <li><a href='#taulua'><img src='http://toi24h.com/life/taulua.gif' title='đi, tàu lửa' /></a></li>
                        <li><a href='#tram'><img src='http://toi24h.com/life/tram.gif' title='trạm đợi, chờ' /></a></li>
                        <li><a href='#thungrac'><img src='http://toi24h.com/life/thungrac.gif' title='thùng rác' /></a></li>
                        <li><a href='#xemtivi'><img src='http://toi24h.com/life/xemtivi.gif' title='Xem ti vi' /></a></li>
                        <li><a href='#quanlot'><img src='http://toi24h.com/life/quanlot.gif' title='quần lót' /></a></li>
                        <li><a href='#bansung'><img src='http://toi24h.com/life/bansung.gif' title='bắn súng, bạo lực' /></a></li>
                        <li><a href='#quayphim'><img src='http://toi24h.com/life/quayphim.gif' title='quay phim' /></a></li>
                        <li><a href='#bangvideo'><img src='http://toi24h.com/life/bangvideo.gif' title='xem, băng video' /></a></li>
                        <li><a href='#diacd'><img src='http://toi24h.com/life/diacd.gif' title='đĩa cd, vcd, đvd' /></a></li>
                        <li><a href='#dibo'><img src='http://toi24h.com/life/dibo.gif' title='đi bộ' /></a></li>
                        <li><a href='#dongho'><img src='http://toi24h.com/life/dongho.gif' title='đồng hồ' /></a></li>
        
                        <li><a href='#ruou'><img src='http://toi24h.com/life/ruou.gif' title='rượu' /></a></li>
                        <li><a href='#zippo'><img src='http://toi24h.com/life/zippo.gif' title='quẹt lửa, zippo' /></a></li></ul>");  
        $this->raw("<script type='text/javascript'>
                    $(document).ready( function() {  
                        $('#icontool').contextMenu({
                            menu: 'myMenu'
                    },
                    function(action) {
                        $('#notice_data-text').replaceSelection(':'+action, true);                    
                    });
                });    
                </script>");
        $this->elementStart('div', array('id' => 'footer'));
        $this->showSecondaryNav();
        $this->showLicenses();
        $this->elementEnd('div');
        //For Google Analytics
         $this->raw('<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-7010797-2");
pageTracker._trackPageview();
} catch(err) {}</script>');

    }

    /**
     * Show secondary navigation.
     *
     * @return nothing
     */
    function showSecondaryNav()
    {
        $this->elementStart('dl', array('id' => 'site_nav_global_secondary'));
        $this->element('dt', null, _('Secondary site navigation'));
        $this->elementStart('dd', null);
        $this->elementStart('ul', array('class' => 'nav'));
        $this->menuItem(common_local_url('doc', array('title' => 'help')),
                        _('Help'));
        $this->menuItem(common_local_url('doc', array('title' => 'about')),
                        _('About'));
        $this->menuItem(common_local_url('doc', array('title' => 'faq')),
                        _('FAQ'));
        $this->menuItem(common_local_url('doc', array('title' => 'api')),
                        _('API'));
        $this->menuItem(common_local_url('doc', array('title' => 'privacy')),
                        _('Privacy'));
        $this->menuItem(common_local_url('doc', array('title' => 'source')),
                        _('Source'));
        $this->menuItem(common_local_url('doc', array('title' => 'contact')),
                        _('Contact'));
        $this->elementEnd('ul');
        $this->elementEnd('dd');
        $this->elementEnd('dl');
    }

    /**
     * Show licenses.
     *
     * @return nothing
     */
    function showLicenses()
    {
        $this->elementStart('dl', array('id' => 'licenses'));
       // $this->showLaconicaLicense();
        $this->showContentLicense();
        $this->elementEnd('dl');
    }

    /**
     * Show Laconica license.
     *
     * @return nothing
     */
    function showLaconicaLicense()
    {
        $this->element('dt', array('id' => 'site_laconica_license'), _('Laconica software license'));
        $this->elementStart('dd', null);
        if (common_config('site', 'broughtby')) {
            $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
        } else {
            $instr = _('**%%site.name%%** is a microblogging service. ');
        }
        $instr .= sprintf(_('It runs the [Laconica](http://laconi.ca/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), LACONICA_VERSION);
        $output = common_markup_to_html($instr);
        $this->raw($output);
        $this->elementEnd('dd');
        // do it
    }

    /**
     * Show content license.
     *
     * @return nothing
     */
    function showContentLicense()
    {
        $this->element('dt', array('id' => 'site_content_license'), _('Laconica software license'));
        $this->elementStart('dd', array('id' => 'site_content_license_cc'));
        $this->elementStart('p');
        $this->element('img', array('id' => 'license_cc',
                                    'src' => common_config('license', 'image'),
                                    'alt' => common_config('license', 'title')));
        //TODO: This is dirty: i18n
        $this->text(_('Tất cả nội dung trên '.common_config('site', 'name').' đều tuân thủ theo quy định tại '));
        $this->element('a', array('class' => 'license',
                                  'rel' => 'external license',
                                  'href' => common_config('license', 'url')),
                       common_config('license', 'title'));
        $this->text(_('.'));
        $this->elementEnd('p');
        $this->elementEnd('dd');
    }

    /**
     * Return last modified, if applicable.
     *
     * MAY override
     *
     * @return string last modified http header
     */
    function lastModified()
    {
        // For comparison with If-Last-Modified
        // If not applicable, return null
        return null;
    }

    /**
     * Return etag, if applicable.
     *
     * MAY override
     *
     * @return string etag http header
     */
    function etag()
    {
        return null;
    }

    /**
     * Return true if read only.
     *
     * MAY override
     *
     * @return boolean is read only action?
     */
    function isReadOnly()
    {
        return false;
    }

    /**
     * Returns query argument or default value if not found
     *
     * @param string $key requested argument
     * @param string $def default value to return if $key is not provided
     *
     * @return boolean is read only action?
     */
    function arg($key, $def=null)
    {
        if (array_key_exists($key, $this->args)) {
            return $this->args[$key];
        } else {
            return $def;
        }
    }

    /**
     * Returns trimmed query argument or default value if not found
     *
     * @param string $key requested argument
     * @param string $def default value to return if $key is not provided
     *
     * @return boolean is read only action?
     */
    function trimmed($key, $def=null)
    {
        $arg = $this->arg($key, $def);
        return is_string($arg) ? trim($arg) : $arg;
    }

    /**
     * Handler method
     *
     * @param array $argarray is ignored since it's now passed in in prepare()
     *
     * @return boolean is read only action?
     */
    function handle($argarray=null)
    {
        $lm   = $this->lastModified();
        $etag = $this->etag();
        if ($etag) {
            header('ETag: ' . $etag);
        }
        if ($lm) {
            header('Last-Modified: ' . date(DATE_RFC1123, $lm));
            $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
            if ($if_modified_since) {
                $ims = strtotime($if_modified_since);
                if ($lm <= $ims) {
                    if (!$etag ||
                        $this->_hasEtag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) {
                        header('HTTP/1.1 304 Not Modified');
                        // Better way to do this?
                        exit(0);
                    }
                }
            }
        }
    }

    /**
     * HasÂ etag? (private)
     *
     * @param string $etag          etag http header
     * @param string $if_none_match ifNoneMatch http header
     *
     * @return boolean
     */
    function _hasEtag($etag, $if_none_match)
    {
        return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
    }

    /**
     * Boolean understands english (yes, no, true, false)
     *
     * @param string $key query key we're interested in
     * @param string $def default value
     *
     * @return boolean interprets yes/no strings as boolean
     */
    function boolean($key, $def=false)
    {
        $arg = strtolower($this->trimmed($key));

        if (is_null($arg)) {
            return $def;
        } else if (in_array($arg, array('true', 'yes', '1'))) {
            return true;
        } else if (in_array($arg, array('false', 'no', '0'))) {
            return false;
        } else {
            return $def;
        }
    }

    /**
     * Server error
     *
     * @param string  $msg  error message to display
     * @param integer $code http error code, 500 by default
     *
     * @return nothing
     */
    function serverError($msg, $code=500)
    {
        $action = $this->trimmed('action');
        common_debug("Server error '$code' on '$action': $msg", __FILE__);
        common_server_error($msg, $code);
    }

    /**
     * Client error
     *
     * @param string  $msg  error message to display
     * @param integer $code http error code, 400 by default
     *
     * @return nothing
     */
    function clientError($msg, $code=400)
    {
        $action = $this->trimmed('action');
        common_debug("User error '$code' on '$action': $msg", __FILE__);
        common_user_error($msg, $code);
    }

    /**
     * Returns the current URL
     *
     * @return string current URL
     */
    function selfUrl()
    {
        $action = $this->trimmed('action');
        $args   = $this->args;
        unset($args['action']);
        foreach (array_keys($_COOKIE) as $cookie) {
            unset($args[$cookie]);
        }
        return common_local_url($action, $args);
    }

    /**
     * Generate a menu item
     *
     * @param string  $url         menu URL
     * @param string  $text        menu name
     * @param string  $title       title attribute, null by default
     * @param boolean $is_selected current menu item, false by default
     * @param string  $id          element id, null by default
     *
     * @return nothing
     */
    function menuItem($url, $text, $title=null, $is_selected=false, $id=null)
    {
        // Added @id to li for some control.
        // XXX: We might want to move this to htmloutputter.php
        $lattrs = array();
        if ($is_selected) {
            $lattrs['class'] = 'current';
        }

        (is_null($id)) ? $lattrs : $lattrs['id'] = $id;

        $this->elementStart('li', $lattrs);
        $attrs['href'] = $url;
        if ($title) {
            $attrs['title'] = $title;
        }
        $this->element('a', $attrs, $text);
        $this->elementEnd('li');
    }

    /**
     * Generate pagination links
     *
     * @param boolean $have_before is there something before?
     * @param boolean $have_after  is there something after?
     * @param integer $page        current page
     * @param string  $action      current action
     * @param array   $args        rest of query arguments
     *
     * @return nothing
     */
    function pagination($have_before, $have_after, $page, $action, $args=null)
    {
        // Does a little before-after block for next/prev page
        if ($have_before || $have_after) {
            $this->elementStart('div', array('class' => 'pagination'));
            $this->elementStart('dl', null);
            $this->element('dt', null, _('Pagination'));
            $this->elementStart('dd', null);
            $this->elementStart('ul', array('class' => 'nav'));
        }
        if ($have_before) {
            $pargs   = array('page' => $page-1);
            $newargs = $args ? array_merge($args, $pargs) : $pargs;
            $this->elementStart('li', array('class' => 'nav_prev'));
            $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
                           _('After'));
            $this->elementEnd('li');
        }
        if ($have_after) {
            $pargs   = array('page' => $page+1);
            $newargs = $args ? array_merge($args, $pargs) : $pargs;
            $this->elementStart('li', array('class' => 'nav_next'));
            $this->element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
                           _('Before'));
            $this->elementEnd('li');
        }
        if ($have_before || $have_after) {
            $this->elementEnd('ul');
            $this->elementEnd('dd');
            $this->elementEnd('dl');
            $this->elementEnd('div');
        }
    }
}
